package cn.gok.homework;

import java.util.*;
import java.util.stream.Stream;

class Book implements Comparable<Book> {
    private int bid;
    private String bname;
    private float price;

    public int getBid() {
        return bid;
    }

    public void setBid(int bid) {
        this.bid = bid;
    }

    public String getBname() {
        return bname;
    }

    public void setBname(String bname) {
        this.bname = bname;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    public Book(int bid, String bname, float price) {
        this.bid = bid;
        this.bname = bname;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bid=" + bid +
                ", bname='" + bname + '\'' +
                ", price=" + price +
                '}';
    }

    @Override
    public int compareTo(Book o) {
        if (this.getPrice() > o.getPrice()) {
            return -1;
        } else if (this.getPrice() == o.getPrice()) {
            return 0;
        }
        return 1;
    }
}

public class Main {
    public static void main(String[] args) {
        作业一();
        作业二();
    }

    private static void 作业二() {
        System.out.println("====================================");
        ArrayList<Book> list = new ArrayList<>();
        list.add(new Book(3, "西游记1", 190));
        list.add(new Book(1, "西游记2", 20));
        list.add(new Book(2, "西游记3", 60));
        list.add(new Book(4, "西游记4", 0));
        list.add(new Book(5, "西游记5", 73));
        Stream<Book> stream = list.stream();
        stream.filter(book -> book.getPrice()<150).sorted(new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                if (o1.getBid() < o2.getBid()) {
                    return -1;
                } else if (o1.getBid()== o2.getBid()) {
                    return 0;
                }
                return 1;
            }

        }).distinct().forEach(System.out::println);
    }


    private static void 作业一() {
        TreeMap<Book, String> map = new TreeMap<>();
        map.put(new Book(1, "西游记1", 100), "老白1");
        map.put(new Book(2, "西游记2", 20), "老白2");
        map.put(new Book(3, "西游记3", 60), "老白3");
        map.put(new Book(4, "西游记4", 0), "老白4");
        map.put(new Book(5, "西游记5", 73), "老白5");
        Iterator<Book> it = map.keySet().iterator();
        while (it.hasNext()) {
            Book key = it.next();
            String value = map.get(key);
            System.out.println(key + "---" + value);
        }
        System.out.println("====================================");
        Iterator<Map.Entry<Book, String>> it2 = map.entrySet().iterator();
        while (it2.hasNext()) {
            Map.Entry<Book, String> e = it2.next();
            System.out.println(e.getKey() + "---" + e.getValue());
        }
    }
}
